package com.sky.happygos.common.YiChart;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

import com.sky.happygos.R;
import com.sky.happygos.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qqqqqqq on 2017/4/23.
 */

public class YiChart extends HorizontalScrollView {
    LayoutInflater layoutInflater;
    LinearLayout container;
    List<ChartItem> views = new ArrayList<>();
    List<Float> datas;
    int count;

    public YiChart(Context context) {
        super(context);
        init();
    }

    public YiChart(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public YiChart(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }



    public void init(){
        container = new LinearLayout(getContext());
        container.setOrientation(LinearLayout.HORIZONTAL);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT);
        container.setLayoutParams(layoutParams);
        addView(container);
        layoutInflater = LayoutInflater.from(getContext());


    }

    public void setCount(List<String> titles){
        this.count = titles.size();
        if(count > 0){
            for(int i = 0;i < count;i++){
                ChartItem view = new ChartItem(getContext());
                view.setTitle(titles.get(i));
                view.setLayoutParams(new LinearLayout.LayoutParams(DensityUtil.dip2px(getContext(),40),ViewGroup.LayoutParams.MATCH_PARENT));
                container.addView(view);
                views.add(view);
            }
        }
    }

    public void setData(List<Float> datas){
        this.datas = datas;
        processData();
    }

    public void processData(){
        int heightPx = DensityUtil.dip2px(getContext(),184);

        float max = 0;
        for(Float data:datas){
            if(data.intValue() > max){
                max = data.floatValue();
            }
        }

        float each = (heightPx-50)/(float)max;
        List<Integer> processData = new ArrayList<>();
        for(Float data:datas){
            float d = data.floatValue();
            int processD = heightPx -(int) (d*each);
            processData.add(new Integer(processD));
        }


        for(int i = 0;i< processData.size();i++){

            int value = processData.get(i).intValue();
            ChartItem item = views.get(i);
            if(i == 0) {
                int nextValue = processData.get(i + 1).intValue();
                item.setData(-1,value,nextValue);
            }else{
                if(i == (datas.size()-1)){
                    int preValue = processData.get(i - 1).intValue();
                    item.setData(preValue,value,-1);
                }else{
                    int preValue = processData.get(i - 1).intValue();
                    int nextValue = processData.get(i + 1).intValue();
                    item.setData(preValue,value,nextValue);
                }
            }
        }
    }

}
